package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.BuKipItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class BuKipThread extends BasicThread{
	public interface BuKipThreadCallback{
		public void OnBuKipCompleted(boolean isNetworkConntected, boolean results
				,String message);
		public void OnBuKipError(ErrorItem buKipItem);
	}
	
	private String url;
	private BuKipThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	// 파라메터
	private String remCd;
	
	public BuKipThread(Context context,	BuKipThreadCallback callback, String remCd
			, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.remCd = remCd;
	}
	
	@Override
	public void run() {
		Map<String,String> map = new HashMap<String, String>();
		map.put(Const.REMCD, remCd);
		String param = "";
		param = parameterReturn(Const.REQ044, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnBuKipCompleted(false, false, "통신 에러");
			return;
		}
		try {
			json = new JSONObject(returnJson);
			if(json == null) {// json 값이 null 일때
				callback.OnBuKipError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			ArrayList<BuKipItem> arrBuKipItem = new ArrayList<BuKipItem>();
			
			if(result == 0){// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);
				callback.OnBuKipError(errorItem);
			}else{// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				BuKipItem buKipItem = null;
				for (int i = 0; i < arrayLogin.length(); i++) {
					JSONObject prod = arrayLogin.getJSONObject(i);
					
					buKipItem = new BuKipItem();
					buKipItem.setBukipCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, BuKipItem.BUKIPCD)));
					buKipItem.setBukipSe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, BuKipItem.BUKIPSE)));
					buKipItem.setNatNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, BuKipItem.NATNM)));
					buKipItem.setItmNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, BuKipItem.ITMNM)));
					buKipItem.setBukipNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, BuKipItem.BUKIPNM)));
					buKipItem.setRegDate(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(prod, BuKipItem.REGDATE)));
					
					arrBuKipItem.add(buKipItem);
				}
				appStatus.setArrBuKipItem(arrBuKipItem);
				callback.OnBuKipCompleted(true, true, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		/*JSONParser jParser = new JSONParser();

		url = Const.MAIN_URL;
		post = ParameterUtil.getBuKipParams(remCd);
		JSONObject json = jParser.getJSONFromUrl(url, post);
		
		Log.i("url","url : "+url);
		Log.i("post","post : "+post);
		Log.i("json","json : "+json);*/
		
		

		/*try {		
			
		} catch (JSONException e) {
		}*/
	}
	
}
